﻿EnsureScript("SP.js");
Type.registerNamespace('VH.SharePoint.Api.UI');
VH.SharePoint.Api.UI.PostPageComponent = function() {
    VH.SharePoint.Api.UI.PostPageComponent.initializeBase(this);
};
VH.SharePoint.Api.UI.PostPageComponent.initializePageComponent = function () {
    ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, function() {
        var ribbonPageManager = SP.Ribbon.PageManager.get_instance();
        if (ribbonPageManager !== null) {
            ribbonPageManager.addPageComponent(VH.SharePoint.Api.UI.PostPageComponent.instance);
        }
    }), 'SP.Ribbon.js');
    
};
VH.SharePoint.Api.UI.PostPageComponent.prototype = {
    initialize: function () {
        this.webProperties = {};
        this.handledCommands = {};
        this.handledCommands['VH.SharePoint.Api.Command.NewPost'] = this.newPost;

        // Array of commands that can be handled that are associated with canHandler methods.
        this.canHandledCommands = {};
        this.canHandledCommands['VH.SharePoint.Api.Command.NewPost'] = this.newPost_canExecute;

        // Array of commands.
        this.commandList = ['VH.SharePoint.Api.Command.NewPost'];
        this.getWebProperties();
    },
    getId: function () { return "PostPageComponent"; },
    getFocusedCommands: function () { return []; },
    getGlobalCommands: function () { return this.commandList; },
    handleCommand: function (commandId, properties, sequence) {
        return this.handledCommands[commandId](commandId, properties, sequence);
    },
    canHandleCommand: function (commandId) {
        var canHandle = this.handledCommands[commandId];
        if (canHandle)
            return this.canHandledCommands[commandId]();
        else
            return false;
    }
};
VH.SharePoint.Api.UI.PostPageComponent.prototype.getWebProperties = function () {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var properties = web.get_allProperties();
    context.load(properties);
    context.executeQueryAsync(function () {
        var p = properties.get_objectData().get_methodReturnObjects().$m_dict;

        function value(key, defaultValue) {
            return typeof (p[key]) == "undefined" ? defaultValue : p[key];
        }

        VH.SharePoint.Api.UI.PostPageComponent.webProperties.blogEnabled = eval(value("blog.Enabled", false));
        VH.SharePoint.Api.UI.PostPageComponent.webProperties.blogPostLibId = eval(value("blog.PostLibrary", "\"00000000-0000-0000-0000-000000000000\""));
    });

};

VH.SharePoint.Api.UI.PostPageComponent.prototype.newPost = function (commandId, properties, sequence) {
    var listId = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    context.load(web);
    context.executeQueryAsync(function () {
        var stsOpen = StsOpenEnsureEx2("SharePoint.OpenDocuments.3");
        if (stsOpen == null) {
            alert(L_NewBlogPost_Text);
            return;
        }
        try {
            var webId = web.get_id().toString();
            var webUrl = web.get_serverRelativeUrl().replace(/\/$/, '');
            webUrl = window.location.protocol + "//" + window.location.host + webUrl;
            var fRet = stsOpen.NewBlogPost("00000000-0000-0000-0000-000000000000", webUrl + "/_vti_bin/api/blog.ashx", webId + "#" + listId);
            return fRet;
        }
        catch (e) {
            alert(L_NewBlogPostFailed_Text);
        }
    });
};

VH.SharePoint.Api.UI.PostPageComponent.prototype.newPost_canExecute = function () {
    if (!VH.SharePoint.Api.UI.PostPageComponent.webProperties.blogEnabled || (new SP.Guid(_spPageContextInfo.pageListId)).toString() != (new SP.Guid(VH.SharePoint.Api.UI.PostPageComponent.webProperties.blogPostLibId)).toString()) return false;
    try {
        var stsOpen = StsOpenEnsureEx2("SharePoint.OpenDocuments.3");
        if (stsOpen == null) {
            return false;
        }
    } catch (e) {
        return false;
    }
    return true;
};
VH.SharePoint.Api.UI.PostPageComponent.webProperties = {};

VH.SharePoint.Api.UI.PostPageComponent.registerClass('VH.SharePoint.Api.UI.PostPageComponent', CUI.Page.PageComponent);

function PageComponentCallback(dialogResult, returnValue) {
    SP.UI.Notify.addNotification(returnValue);
    SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}

VH.SharePoint.Api.UI.PostPageComponent.instance = new VH.SharePoint.Api.UI.PostPageComponent();
VH.SharePoint.Api.UI.PostPageComponent.instance.initialize();
VH.SharePoint.Api.UI.PostPageComponent.initializePageComponent();
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("VH.SharePoint.Api.UI.js");
